package com.gyc.simple.sysuser.controller;

import com.alibaba.fastjson.JSONObject;
import com.gyc.simple.common.util.JwtUtils;
import com.gyc.simple.common.util.UUIDUtils;
import com.gyc.simple.common.vo.ResponseType;
import com.gyc.simple.common.vo.User;
import com.gyc.simple.sysuser.entity.base.IRole;
import com.gyc.simple.sysuser.entity.base.ISource;
import com.gyc.simple.sysuser.service.SysResourceService;
import com.gyc.simple.sysuser.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@Api(value = "/sys/role/",tags = "系统角色模块")
@RestController
@RequestMapping("/sys/role/")
public class IRoleController {

    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysResourceService sysResourceService;
    @ApiOperation(value = "获取角色列表",notes = "/getRoleList")
    @GetMapping("/getRoleList")
    public String getRoleList(@RequestParam Map paraMap){
        try {
            return Optional.ofNullable(JSONObject.toJSONString(sysRoleService.queryListByPageCondition(CommonMethod.checkPageParam(paraMap)))).orElse("");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    @GetMapping("/getCurUserRole")
    public ResponseType getCurUserRole(){
        ResponseType responseType = new ResponseType();
        try {
            List<IRole> iRoles = sysRoleService.selectByUID(JwtUtils.JwtToUser().getIuId());
            responseType.setStatus(true);
            responseType.setData(JSONObject.toJSON(iRoles));
        }catch (Exception e){
            e.printStackTrace();
        }
        return responseType;
    }

    @PostMapping("/addRole")
    public ResponseType addRole(@RequestBody IRole iRole){
        ResponseType responseType = new ResponseType();
        try {
            iRole.setIregister(JwtUtils.JwtToUser().getIuId());
            iRole.setIroleid(UUIDUtils.getUUID32());
            sysRoleService.addRole(iRole);
            responseType.setStatus(true);
        }catch (Exception e){
            e.printStackTrace();
        }
        return responseType;
    }
}
